//
//  OrganizationViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/14.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "OrganizationViewController.h"
#import "YYInfiniteLoopView.h"
#import "LearnSkillMiddleCell.h"
#import "ListenStoryHeaderCell.h"
#import "OrganationSeekCell.h"
#import "OrganationBottomCell.h"
@interface OrganizationViewController ()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,YYInfiniteLoopViewDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@end

@implementation OrganizationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"机构";
    [self createCollectionUI];
    // Do any additional setup after loading the view.
}
#pragma mark -图片轮播
- (void)loopViewDelegateAndAddToFatherView:(UIView *)faterView{
    NSArray *array = @[@"1.jpg",@"2.jpg",@"未标题-2.jpg",@"4.jpg",];
    // 类方法
    YYInfiniteLoopView *loopView = [YYInfiniteLoopView
                                    infiniteLoopViewWithImageUrls:array
                                    titles:nil
                                    didSelectedImage:^(NSInteger index) {
                                        NSLog(@"%zd",index);
                                    }];
    // 设置代理
    loopView.delegate = self;
    loopView.timeInterval = 1;
    loopView.currentPageIndicatorColor = [UIColor yellowColor];
    loopView.pagePosition = InfiniteLoopViewPagePositionCenter;
    loopView.frame = CGRectMake(0, 0, WIDTH, scaleTopAndBottom(175));
    [faterView addSubview:loopView];
}
#pragma mark - YYInfiniteLoopViewDelegate
- (void)infiniteLoopView:(YYInfiniteLoopView *)infiniteLoopView
        didSelectedImage:(NSInteger)selectedImageIndex {
    [self didSelectedImageWithIndex:selectedImageIndex];
}

- (void)didSelectedImageWithIndex:(NSInteger)index {
    NSLog(@"点击了%ld",index);
}
#pragma mark -创建collectionView
-(void)createCollectionUI{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderone"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooter"];
    UINib *nib1 = [UINib nibWithNibName:@"ListenStoryHeaderCell" bundle:nil];
    [self.collectionView registerNib:nib1 forCellWithReuseIdentifier:@"ListenStoryHeaderCell"];
    [self.collectionView registerClass:[OrganationSeekCell class] forCellWithReuseIdentifier:@"OrganationSeekCell"];
    [self.collectionView registerClass:[OrganationBottomCell class] forCellWithReuseIdentifier:@"OrganationBottomCell"];
    [self.collectionView reloadData];
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 2 || section == 1) {
        return 10;
    }
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ListenStoryHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListenStoryHeaderCell" forIndexPath:indexPath];
        [self loopViewDelegateAndAddToFatherView:cell.contentView];
        return cell;
    }else if (indexPath.section == 1){
        OrganationSeekCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OrganationSeekCell" forIndexPath:indexPath];
        return cell;
    }else{
        OrganationBottomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OrganationBottomCell" forIndexPath:indexPath];
        return cell;
    }
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(WIDTH, scaleTopAndBottom(175));
    }else if (indexPath.section == 1){
        return CGSizeMake(scaleLeftAndRight(70), scaleTopAndBottom(100));
    }else{
        return CGSizeMake(WIDTH, scaleTopAndBottom(100));
    }
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return scaleTopAndBottom(1);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return scaleLeftAndRight(1);
}
#pragma mark - 注册collectionView的头部视图
//collectionview的头部视图的设置
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 0 || indexPath.section == 2) {
            return nil;
        }else{
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderone" forIndexPath:indexPath];
            for (UIView *subView in view.subviews) {
                [subView removeFromSuperview];
            }
            [self createHeaderViewAboutUI:@"申请机构入驻" AndAddSubviewToFatherview:view];
            return view;
        }
    }else{
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooter" forIndexPath:indexPath];
        view.backgroundColor = [UIColor cyanColor];
        return view;
    }
    
}
//设置头部视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 2) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(WIDTH, scaleTopAndBottom(50));
}
//设置尾视图
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 1 || section == 2) {
        return CGSizeZero;
    }
    return CGSizeMake(WIDTH, scaleTopAndBottom(5));
}
//创建tableview的头部视图
-(UICollectionReusableView *)createHeaderViewAboutUI:(NSString *)headerTitle AndAddSubviewToFatherview:(UICollectionReusableView *)fatherView{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor yellowColor];
    [fatherView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(fatherView.mas_centerY);
        make.left.equalTo(fatherView.mas_left);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(scaleLeftAndRight(2));
    }];
    UILabel *lable = [[UILabel alloc] init];
    [fatherView addSubview:lable];
    lable.text = headerTitle;
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_right).offset(scaleLeftAndRight(10));
        make.top.equalTo(fatherView.mas_top);
        make.bottom.equalTo(fatherView.mas_bottom);
        make.right.equalTo(fatherView.mas_right).offset(scaleLeftAndRight(-10));
    }];
    return fatherView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
